#include <CommonHelper.h>

CommonHelper* CommonHelper::Instance=0;

CommonHelper* CommonHelper::commonInstance()
{
    if(Instance==0)
        Instance =new CommonHelper();
    return Instance;
}

CommonHelper::CommonHelper()
{
    logClient=loghelper::getInstance();
}

QString CommonHelper::GetGuid(){
    try{
        QUuid id = QUuid::createUuid();
        QString strGuid =id.toString(QUuid::Id128);
        return  strGuid;
    }
    catch(std::exception &e){
        QString err=e.what();
        logClient->WriteLog("Exception","获取GUID异常:"+err);
    }
    catch(...)
    {
        logClient->WriteLog("Exception","获取GUID捕获到未知异常;");
    }

}

QString CommonHelper::GetDateTimeStr(){
    try{
        QDateTime current=QDateTime::currentDateTime();
        QString dateStr=current.toString("yyyy-MM-dd hh:mm:ss");
        return dateStr;
    }
    catch(std::exception &e){
        QString err=e.what();
        logClient->WriteLog("Exception","获取时间字符串异常:"+err);
    }
    catch(...)
    {
        logClient->WriteLog("Exception","获取时间字符串捕获到未知异常;");
    }

}

QString CommonHelper::TenToSixteen(int num, int bit){
    try{
        QString hex = QString::number(num, 16);
        if (hex.length() < bit) {
            int n = bit - hex.length();
            for (int i = 0; i < n; i++) {
                hex.prepend("0");
            }
        }

        QString result;
        for (int i = 0; i < hex.length(); ++i) {
            result.append(hex.at(i));
            if (i % 2 == 1 && i != hex.length() - 1) {
                result.append(" ");
            }
        }
        return result;
    }
    catch(std::exception &e){
        QString err=e.what();
        logClient->WriteLog("Exception","十进制转十六进制异常:"+err);
    }
    catch(...)
    {
        logClient->WriteLog("Exception","十进制转十六进制捕获到未知异常;");
    }
}

QByteArray CommonHelper::GetSendData(QString sendData){

    sendData= sendData.trimmed();
    QByteArray result;
    for(int i = 0;i<sendData.size();i++)
    {
        if(sendData[i]==' ')
            continue;   //跳过
        int num  = sendData.mid(i,2).toUInt(nullptr,16);
        i++;
        result.append(num);
    }
    return result;

}

QString CommonHelper::GetCrcCheck(QByteArray data) {
    try{
        quint16 polynomial = 0xA001;
        quint16 crc = 0xFFFF;
        for (const char byte : data) {
            crc ^= (quint16)(byte << 8);
            for (int bit = 0; bit < 8; ++bit) {
                if (crc & 0x8000) {
                    crc = (crc << 1) ^ polynomial;
                } else {
                    crc = crc << 1;
                }
            }
        }
        return TenToSixteen(crc,4);
    }
    catch(std::exception &e){
        QString err=e.what();
        logClient->WriteLog("Exception","转Crc字符串异常:"+err);
    }
    catch(...)
    {
        logClient->WriteLog("Exception","转Crc字符串捕获到未知异常;");
    }
}

QString CommonHelper::SixteenToTen(QString data){
    try{
        data.replace(" ","");
        bool ok;
        int dec = data.toInt(&ok,16);
        QString num = QString("%1").arg(dec,0,10);
        return  num;
    }
    catch(std::exception &e){
        QString err=e.what();
        logClient->WriteLog("Exception","十六进制转十进制异常:"+err);
    }
    catch(...)
    {
        logClient->WriteLog("Exception","十六进制转十进制捕获到未知异常;");
    }
}

QString CommonHelper::SixteenToTwo(QString data){
    QString bin = QString::number(data.toInt(NULL, 16), 2);
    return bin;
}
